package com.hzw.code

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.lifecycleScope
import com.hzw.code.pageState.OnRetryListener
import com.hzw.code.pageState.OnShowHideViewListener
import com.hzw.code.pageState.PageStateManager
import com.hzw.code.pageState.State
import com.safframework.log.L
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_page_state.*
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class PageStateActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_page_state)
        val pageStateManager = PageStateManager.Builder(this)
            .loadingLayoutResId()
            .contentLayoutResId(R.layout.activity_page_content)
            .errorView(
                onRefreshListener = OnRefreshListener { L.i("错误界面刷新") },
                onRetryListener = object : OnRetryListener {
                    override fun onRetry() {
                        L.i("错误界面重试")
                    }
                }
            )
            .emptyView(
                onRefreshListener = OnRefreshListener {
                    lifecycleScope.launch{
                        delay(2*1000)
                        it.finishRefresh()
                    }
                    L.i("空界面刷新")
                },
                onRetryListener = object : OnRetryListener {
                    override fun onRetry() {
                        L.i("空界面重试")
                    }
                }
            )
            .netErrorView(
                onRefreshListener = OnRefreshListener { L.i("网络错误界面刷新") },
                onRetryListener = object : OnRetryListener {
                    override fun onRetry() {
                        L.i("网络错误界面重试")
                    }
                }
            )
            .onShowHideViewListener(object : OnShowHideViewListener {
                override fun onShowView(view: View, state: State) {
                    L.i("show => view = $view, state = $state")
                }

                override fun onHideView(view: View, state: State) {
                    L.i("hide => view = $view, state = $state")
                }

            })
            .build()
        container.addView(pageStateManager.rootLayout)

        showContent.setOnClickListener {
            pageStateManager.showContentView()
        }

        showLoading.setOnClickListener {
            pageStateManager.showLoading()
        }

        hideLoading.setOnClickListener {
            pageStateManager.hideLoading()
        }

        showEmpty.setOnClickListener {
            pageStateManager.showEmptyView()
        }

        showError.setOnClickListener {
            pageStateManager.showErrorView()
        }

        showNetError.setOnClickListener {
            pageStateManager.showNetErrorView(
                imageResId = R.drawable.ic_cloud_off,
                tipText = "网络异常，请检查网络",
                retryText = "去检查网络"
            )
        }
    }
}
